package com.ax.api.route.business;

import com.ax.api.route.util.RouteConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @Author Lg
 * @Date 2018/9/6 10:00
 * @Content
 */
@RestController
@RequestMapping("/web/HempRegiste/")
public class HempRegisteController {
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;

    /**
     * 含麻登记(模块:含麻)
     * @param imageFile
     * @return
     */
    @RequestMapping("insertHemp")
    public ResponseEntity<String> insertHemp(@RequestParam Map<String,Object> map){
        String url = geturl("/insertHemp", RouteConfig.BUSSINESS_ROUTE);
        return restTemplate.postForEntity(url,map,String.class);
    }

    /**
     * 含麻查询(模块:含麻)
     * @param imageFile
     * @return
     */
    @RequestMapping("queryHemp")
    public ResponseEntity<String> queryHemp(@RequestParam Map<String,Object> map){
        String url = geturl("/queryHemp", RouteConfig.BUSSINESS_ROUTE);
        return restTemplate.postForEntity(url,map,String.class);
    }

    /**
     * 含麻修改状态
     * @param imageFile
     * @return
     */
    @RequestMapping("updateFlag")
    public ResponseEntity<String> updateFlag(@RequestParam Map<String,Object> map){
        String url = geturl("/updateFlag", RouteConfig.BUSSINESS_ROUTE);
        return restTemplate.postForEntity(url,map,String.class);
    }

    /**
     * 修改含麻
     * @param imageFile
     * @return
     */
    @RequestMapping("updateHempRegisteDetail")
    public ResponseEntity<String> updateHempRegisteDetail(@RequestParam Map<String,Object> map){
        String url = geturl("/updateHempRegisteDetail", RouteConfig.BUSSINESS_ROUTE);
        return restTemplate.postForEntity(url,map,String.class);
    }

    /**
     * 删除含麻
     * @param imageFile
     * @return
     */
    @RequestMapping("deleteHempRegisteDetail")
    public ResponseEntity<String> deleteHempRegisteDetail(@RequestParam Map<String,Object> map){
        String url = geturl("/deleteHempRegisteDetail", RouteConfig.BUSSINESS_ROUTE);
        return restTemplate.postForEntity(url,map,String.class);
    }

    /**
     * 根据药品名查询药店药品
     * @param ajaxRes
     * @param map
     * @return
     */
    @RequestMapping("queryBaseDrugDto")
    public ResponseEntity<String> queryBaseDrugDto(@RequestParam Map<String,Object> map){
        String url = geturl("/BaseDrugDto/queryBaseDrugDto",RouteConfig.BUSSINESS_ROUTE);
        return restTemplate.postForEntity(url,map,String.class);
    }



    public String geturl(String path, String config){
        ServiceInstance serviceInstance = this.loadBalancerClient.choose(config);
        //	 String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort()+path;
        StringBuffer bf = new StringBuffer("http://");
        bf.append(serviceInstance.getHost());
        bf.append(":");
        bf.append(serviceInstance.getPort());
        bf.append(path);
        return bf.toString();
    }



}
